﻿using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace RunGun
{
    /// <summary>
    /// Manages loading, unloading, and shaders for 3d models.
    /// </summary>
    public sealed class ModelManager : ModelManagerBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ModelManager"/> class.
        /// </summary>
        /// <param name="contentManager">The content manager.</param>
        public ModelManager(ContentManager contentManager) : base(contentManager)
        {
        }

        /// <summary>
        /// Loads the model and returns the instance.
        /// </summary>
        /// <typeparam name="T">The type for the shader</typeparam>
        /// <param name="modelName">Name of the model.</param>
        /// <param name="textureName">Name of the texture.</param>
        /// <param name="shaderName">Name of the shader.</param>
        public override void LoadModel<T>(string modelName, string textureName, string shaderName)
        {
            var gameModel = new GameModel(ContentManager.Load<Model>(modelName),
                                          ContentManager.Load<Texture2D>(textureName),
                                          LoadEffect<T>(shaderName));
            gameModels.Add(gameModel);
        }

        /// <summary>
        /// Loads the shader effect with the given name.
        /// </summary>
        /// <param name="effectName">Name of the effect.</param>
        /// <returns>An IShaderEffect object with the specified shader effect.</returns>
        public override T LoadEffect<T>(string effectName)
        {
            var shaderEffect = new T { Effect = ContentManager.Load<Effect>(effectName) };
            return shaderEffect;
        }

        /// <summary>
        /// Unloads the model from memory.
        /// </summary>
        /// <param name="modelName">Name of the model.</param>
        public override void UnloadModel(string modelName)
        {
        }

        /// <summary>
        /// Draws the game models.
        /// </summary>
        /// <param name="camera">The game camera.</param>
        public override void Draw(CameraBase camera)
        {
            foreach (var model in gameModels)
            {
                model.Draw(camera);
            }
        }
    }
}